#!/usr/bin/env python
# coding: utf-8


import ConfigParser
import sys

reload(sys)
sys.setdefaultencoding('utf-8')
from wxbot import *


class MyWXBot(WXBot):
    def handle_msg_all(self, msg):
        if msg['msg_type_id'] == 4 and msg['content']['type'] == 0:
            content = msg['content']['data']
            # print content
            content = content.split(":", 1)
            # print json.dumps(content)
            if content[0] == 'groupcreate':
                self.batchCreateGroup(content[1])
        elif msg['msg_type_id'] == 99 and msg['content']['type'] == 0:
            content = msg['content']['data']
            # print content
            content = content.split(":", 1)
            # print json.dumps(content)
            if content[0] == 'groupcreate':
                self.batchCreateGroup(content[1])

    # 批量建群
    def batchCreateGroup(self, groupName):
        conf = ConfigParser.ConfigParser()
        conf.read("img/create_group.conf")
        group_count = int(conf.get("group", "group_count"))
        nick_names = conf.get("group", "nick_names").split(",")
        members = []
        for k, v in enumerate(self.contact_list):
            if v["NickName"] in nick_names:
                members.append({"UserName": v["UserName"]})
        start_factor = int(conf.get("group", "start_factor"))
        growth_factor = int(conf.get("group", "growth_factor"))
        myi = start_factor
        for i in range(0, group_count):
            newGroupName = groupName + "_" + str(myi)
            self.createRoom(members, newGroupName)
            myi = myi + growth_factor
            time.sleep(5)


def main():
    bot = MyWXBot()
    bot.DEBUG = True
    bot.conf['qr'] = 'png'
    # bot.is_big_contact = False   #如果确定通讯录过大，无法获取，可以直接配置，跳过检查。假如不是过大的话，这个方法可能无法获取所有的联系人
    bot.run()


if __name__ == '__main__':
    main()
